<?php

/* 
 * EachGo电商网站 新媒体
 * www.eachgo.cn
 */
namespace Home\Model;
use Think\Model;
class LotteryModel extends Model{    

    public function lotteryNumber($userId){
        $sqlOneGot="SELECT COUNT(*) AS oneNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=1 AND isGot=TRUE";
        $sqlTwoGot="SELECT COUNT(*) AS twoNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=2 AND isGot=TRUE";
        $sqlFiveGot="SELECT COUNT(*) AS fiveNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=3 AND isGot=TRUE";
        $sqlTenGot="SELECT COUNT(*) AS tenNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=4 AND isGot=TRUE";
        $oneNumberGot=$this->query($sqlOneGot);
        $twoNumberGot=$this->query($sqlTwoGot);
        $fiveNumberGot=$this->query($sqlFiveGot);
        $tenNumberGot=$this->query($sqlTenGot);

        $sqlOneUse="SELECT COUNT(*) AS oneNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=1 AND isGot=FALSE";
        $sqlTwoUse="SELECT COUNT(*) AS twoNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=2 AND isGot=FALSE";
        $sqlFiveUse="SELECT COUNT(*) AS fiveNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=3 AND isGot=FALSE";
        $sqlTenUse="SELECT COUNT(*) AS tenNumber FROM EachGo.`t_lottery` WHERE user_ID=".$userId." AND LEVEL=4 AND isGot=FALSE";
        $oneNumberUse=$this->query($sqlOneUse);
        $twoNumberUse=$this->query($sqlTwoUse);
        $fiveNumberUse=$this->query($sqlFiveUse);
        $tenNumberUse=$this->query($sqlTenUse);

        $oneNumber=(int)$oneNumberGot[0]['onenumber']-(int)$oneNumberUse[0]['onenumber'];
        $twoNumber=(int)$twoNumberGot[0]['twonumber']-(int)$twoNumberUse[0]['twonumber'];
        $fiveNumber=(int)$fiveNumberGot[0]['fivenumber']-(int)$fiveNumberUse[0]['fivenumber'];
        $tenNumber=(int)$tenNumberGot[0]['tennumber']-(int)$tenNumberUse[0]['tennumber'];
//        $money=
        //$result=array_merge($oneNumber[0],$twoNumber[0],$fiveNumber[0],$tenNumber[0]);
        return array_merge(array('oneNumber'=>$oneNumber),array('twoNumber'=>$twoNumber),array('fiveNumber'=>$fiveNumber),array('tenNumber'=>$tenNumber));
//        var_dump($oneNumber);
//        var_dump($twoNumber);
//        var_dump($fiveNumber);
//        var_dump($tenNumber);

    }
    public function queryNumber($userId){
        $sql="SELECT COUNT(*) AS number FROM EachGo.`t_lottery` WHERE EachGo.`t_lottery`.`user_ID`=".$userId;
        return $this->query($sql);
    }
    public function queryLottery($userId){
        return $this->where("user_ID=".$userId)->select();
    }
    public function addLottery($data){
        return $this->add($data);
    }
    public function deleteLottery($id){
        return $this->where("ID=".$id)->delete();
    }
    public function queryAll(){
        return $this->select();
    }


    public function isGot($userId){
        return $this->where("user_ID=".$userId." AND isGot=true")->select();
    }
    public function isUsed($userId){
        return $this->where("user_ID=".$userId." AND isGot=false")->select();
    }
    public function update($data){
        return $this->where("ID=".$data['id'])->save($data);
    }
}
